﻿using Aspects;
using PostSharp.Extensibility;
using PostSharp.Laos;
using PostSharp.Laos.Weaver;

namespace Weaver
{
/// <summary>
/// Returns the weaver that will handle an aspect.
/// </summary>
public class WeaverFactory : Task, ILaosAspectWeaverFactory
{
   #region Methods

   /// <summary>
   /// Creates the aspect weaver.
   /// </summary>
   /// <param name="aspect">The aspect.</param>
   /// <returns></returns>
   public LaosAspectWeaver CreateAspectWeaver(ILaosAspect aspect)
   {
      if (aspect is OtherEndFieldSubAspect)
      {
         var oneWayAssociation = (OtherEndFieldSubAspect)aspect;
         return new AddFieldWeaver(oneWayAssociation.FieldTypeName, oneWayAssociation.FieldName,
                                oneWayAssociation.AssociationName);
      }
      if (aspect is AssociationAttributeSubAspect)
      {
         var addAssociationAttribute = (AssociationAttributeSubAspect) aspect;
         return new PropertyAttributeWeaver(addAssociationAttribute.PropertyName,
                                    addAssociationAttribute.AssociationName);
      }

      return null;
   }

   #endregion
}
}